<template>
  <ContainerGroup>
    <Container style="margin-bottom:10px;">
      <FilterForm
        ref="filterForm"
        hand
        :elements="queryForm"
        @reset="handlerReset"
        @submit="handleSearch"
      >
        <Button
          style="display: inline-block; margin-right: 10px"
          @click="handleExport"
        >导出</Button>
      </FilterForm>
    </Container>
    <Container height="fill" display="flexY">
      <Table
        :columns="tableColumns"
        :data="tableData"
        row-key="id"
        :loading="tableLoading"
        :pagination="{
          total: total,
          current: pageNum,
          pageSize: pageSize,
        }"
        @paginationChange="handlePagination"
      />
    </Container>
  </ContainerGroup>
</template>
<script>
import { getAccvouchWorkload, exportExcelPz } from '@/utils/apis/basic'
export default {
  name: '', // 分录凭证
  data() {
    return {
      queryForm: [
        {
          label: '日期',
          key: 'startEndTime',
          type: 'date-picker',
          mode: 'monthrange',
          width: '340',
          expiredDate: {},
          format: 'yyyy-MM',
          value: [this.$moment().format('yyyy-MM'), this.$moment().format('yyyy-MM')]
        }
      ],
      tableData: [],
      tableColumns: [
        {
          title: '制单日期',
          dataIndex: 'dbillDate'
        },
        {
          title: '凭证编号',
          dataIndex: 'csignContact'
        },
        {
          title: '摘要',
          dataIndex: 'cdigest'
        },
        {
          title: '制单人',
          dataIndex: 'cbill'
        },
        {
          title: '审核人',
          dataIndex: 'ccheck'
        },
        {
          title: '审核日期',
          dataIndex: 'dauditDate'
        },
        {
          title: '记账人',
          dataIndex: 'cbook'
        },
        {
          title: '分录数',
          dataIndex: 'total'
        }
      ],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      newConditon: {}
    }
  },
  created() {

  },
  methods: {
    handleExport() { // 导出
      const fieldVal = this.$refs.filterForm.getParms()
      const date = fieldVal?.startEndTime
      const param = {
        startDate: date && date[0] || '',
        endDate: date && date[1] || ''
      }
      exportExcelPz(param).then(res => {
        const blob = new Blob([res.data], {
          type: res.data.type + 'charset=utf-8'
        })
        const downloadLink = document.createElement('a')
        const URL = window.URL || window.webkitURL || window.moxURL
        const downUrl = URL.createObjectURL(blob)
        downloadLink.href = downUrl
        downloadLink.download = '分录凭证.xlsx'
        downloadLink.click()
        window.URL.revokeObjectURL(downUrl)
        // document.body.removeChild(downloadLink) // 删除 a 标签
      })
    },
    handleSearch(value) {
      this.newConditon = value
      this.pageNum = 1
      this.getData()
    },
    handlerReset(value) {
      this.$refs.filterForm.handleForceSet({
        startEndTime: [this.$moment().format('yyyy-MM'), this.$moment().format('yyyy-MM')]
      })
      this.newConditon = this.$refs.filterForm.getParms()
      this.pageNum = 1
      this.getData()
    },
    handlePagination(payload) {
      // 分页
      this.pageNum = payload.current
      this.pageSize = payload.pageSize
      this.getData()
    },
    getData() {
      const param = {
        startDate: this.newConditon.startEndTime && this.newConditon.startEndTime[0] || '',
        endDate: this.newConditon.startEndTime && this.newConditon.startEndTime[1] || '',
        pageIndex: this.pageNum,
        pageSize: this.pageSize
      }
      this.tableLoading = true
      getAccvouchWorkload(param).then((res) => {
        this.tableLoading = false
        if (res.data.code === 200) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
}
</script>
